package DigiJoe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Observable;


public abstract class DigitalElement extends Observable {

    //kimenetek listaja, PORT, rakotott vezetekek
    protected HashMap<Integer, ArrayList<Wire>> outputList = new HashMap<Integer, ArrayList<Wire>>();
    //max kimeneti portok szama
    protected int maxOutputPort;

    //konstruktor
    public DigitalElement (int MaxOutputPort) {
        //max port beallitas
        maxOutputPort = MaxOutputPort;
    }

    //minden kimeneti vezeteket visszaad
    public ArrayList<Wire> GetAllOutput(){
        ArrayList<Wire> temp = new ArrayList<Wire>();
        for(int i = 0; i < outputList.size(); i++){
            temp.addAll(outputList.get(i));
        }
        return temp;
    }

    public int getMaxOutputPort() { return maxOutputPort; }

    //output hozzaadasa
    public void addOutput (int port, Wire output) throws Exception {
        //ellenorzes
        if(port >= maxOutputPort)
            throw new Exception("The port given is too high!");
            //ErrorController.signalError("The port given is too high!");
        if (port < 0)
            throw new Exception("The port given is too low!");
            //ErrorController.signalError("The port given is too low!");

        //ha meg nincsr ilyen port
        if(!outputList.containsKey(port)){
            outputList.put(port, new ArrayList<Wire>());
        }
        //hozzaadjuk a vezeteket
        outputList.get(port).add(output);
    }

    //so we can get the elements type
    public abstract ElementType getType();
}
